function clock() {
    document.getElementById("btn_export").disabled =
        !(document.getElementById("info_table") != null &&
            document.getElementById("div_table").style.display !== "none");
}

function btn_update_show() {
    if(document.getElementById("btn_submit").style.display !== "none")
        document.getElementById("btn_submit").style.display = "none";
    if(document.getElementById("btn_update").style.display !== "inline")
        document.getElementById("btn_update").style.display = "inline";
}

function btn_update_hidden() {
    if(document.getElementById("btn_update").style.display !== "none")
        document.getElementById("btn_update").style.display = "none";
    if(document.getElementById("btn_submit").style.display !== "inline")
        document.getElementById("btn_submit").style.display = "inline";
}

function clear_info_table() {
    document.getElementById("table_id").value = "";
    document.getElementById("table_name").value = "";
    document.getElementsByName("table_gender")[0].checked = true;
    document.getElementById("table_school").value = "";
    document.getElementById("table_college").value = "";
    document.getElementById("table_major").value = "";
    document.getElementById("table_class").value = "";
    document.getElementById("table_tel").value = "";
    document.getElementById("table_origin").value = "";
}

function info_table_show() {
    let div_table = document.getElementById("div_table");
    let div_info_table = document.getElementById("div_info_table");
    if (div_table.style.display !== "none")
        div_table.style.display = "none";
    if (div_info_table.style.display !== "block")
        div_info_table.style.display = "block";
}

function info_table_hidden() {
    let div_table = document.getElementById("div_table");
    let div_info_table = document.getElementById("div_info_table");
    if (div_info_table.style.display !== "none")
        div_info_table.style.display = "none";
    if (div_table.style.display !== "block")
        div_table.style.display = "block";
}

function searchInfo(searchType, search_str) {
    let search = search_str.trim();
    if (search != null && search !== "") {
        sessionStorage.setItem("searchType", searchType);
        sessionStorage.setItem("search", search);
        //alert("searchType:" + searchType_str + "  search:" + search_str);
        const xhr = new XMLHttpRequest();
        let url = "./stuGetInfo.do?searchType=" + searchType + "&search=" + search + "&r=" + Math.random();
        xhr.open("GET", url, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (xhr.responseText !== "false")
                    info_table_hidden();
                else
                    alert("未查询到信息！")
                location.reload();
            }
        }
        xhr.send();
    }
}

function getTableInfo() {
    let id = document.getElementById("table_id").value;
    if (id.trim().length > 0) {
        let username = document.getElementById("table_name").value;
        let radio_group = document.getElementsByName("table_gender");
        let gender = "";
        for (let i = 0; i < radio_group.length; i++)
            if (radio_group[i].checked)
                gender = radio_group[i].value;
        let school = document.getElementById("table_school").value;
        let college = document.getElementById("table_college").value;
        let major = document.getElementById("table_major").value;
        let ofClass = document.getElementById("table_class").value;
        let tel = document.getElementById("table_tel").value;
        let origin = document.getElementById("table_origin").value;

        return {
            id: id, username: username, gender: gender, school: school, college: college,
            major: major, ofClass: ofClass, tel: tel, origin: origin
        };
    }
    return false;
}

function insertInfo() {
    const stu = getTableInfo();
    if (stu) {
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "stuGetInfo.do", false);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (xhr.responseText === "true")
                    alert("添加成功！");
                else if (xhr.responseText === "false")
                    alert("该学号已存在！");
                else
                    alert("添加失败！");
            }
        }
        xhr.send("stuInfo=" + JSON.stringify(stu));
    } else
        alert("学号不得为空！");
}

function updateInfo() {
    const stu = getTableInfo();
    if (stu) {
        let search = sessionStorage.getItem("search");
        let searchType = sessionStorage.getItem("searchType");
        if((search !=null || search !== "") && (searchType!=null || searchType !== "")) {
            const xhr = new XMLHttpRequest();
            xhr.open("POST", "stuInfoUpdate.do", false);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    if (xhr.responseText === "true") {
                        alert("更新成功！");
                        searchInfo(searchType, search);
                    } else
                        alert("更新失败！");
                }
            }
            xhr.send("stuInfo=" + JSON.stringify(stu));
        }
        else
            alert("操作失败！");
    }
}

function deleteById(str) {
    let search = sessionStorage.getItem("search");
    let searchType = sessionStorage.getItem("searchType");

    if((search !=null || search !== "") && (searchType!=null || searchType !== "")) {
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "stuInfoDel.do", false);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (xhr.responseText === "true") {
                    alert("删除成功！");
                    searchInfo(searchType, search);
                } else
                    alert("删除失败！");
            }
        }
        xhr.send("id=" + str);
    } else
        alert("操作失败！");
}

function openUpdateTable(str) {
    info_table_show();
    btn_update_show();
    document.getElementById("table_id").value = document.getElementById("id" + str).innerText;
    document.getElementById("table_id").readOnly = true;
    document.getElementById("table_name").value = document.getElementById("username" + str).innerText;
    if(document.getElementById("gender" + str).innerText === "男")
        document.getElementById("table_gender_male").checked = true;
    else
        document.getElementById("table_gender_female").checked = true;
    document.getElementById("table_school").value = document.getElementById("school" + str).innerText;
    document.getElementById("table_college").value = document.getElementById("college" + str).innerText;
    document.getElementById("table_major").value = document.getElementById("major" + str).innerText;
    document.getElementById("table_class").value = document.getElementById("ofClass" + str).innerText;
    document.getElementById("table_tel").value = document.getElementById("tel" + str).innerText;
    document.getElementById("table_origin").value = document.getElementById("origin" + str).innerText
}

function exportExcel() {
    let search = sessionStorage.getItem("search");
    let searchType = sessionStorage.getItem("searchType");

    if ((search != null || search !== "") && (searchType != null || searchType !== "")) {
        window.location.href = "./export.do?r=" + Math.random();
    } else
        alert("操作失败！");
}
